package Version3;

public class Battle{
	private int totalHunter;
	private int totalMonster;
	private int currentHunter;
	private int currentMonster;
	private boolean isEnd;
	private boolean isCHAlive;
	private boolean isCMAlive;
	private Monster[] m;
	private MonsterHunter[] h;
	private Output o;
	private Stage stage;

	public Battle()
	{
		totalHunter = 1;
		totalMonster = 5;
		currentHunter = 0;
		currentMonster = 0;
		isEnd = false;
		isCHAlive = true;
		isCMAlive = true;
		m = null;
		h = null;
		o = new Output();
		stage = new Stage();
	}

	public Battle (int hc,int mc,Monster[] m,MonsterHunter[] h) 
	{
		
		totalHunter = hc;
		totalMonster = mc;
		currentHunter = 0;
		currentMonster = 0;
		isEnd = false;
		isCHAlive = true;
		isCMAlive = true;
		this.m = m;
		this.h = h;
		o = new Output();
		stage = new Stage();
		startBattle();
	}



	protected void startBattle()
	{
		while(!isEnd)
		{
			if(isCHAlive==true)
				initiateHunterAttack();
			if(isCMAlive==true)
				initiateMonsterAttack();
		}
	}

	protected boolean isHunterWin()
	{
		if(isCMAlive == false && isCHAlive== true && currentMonster == (totalMonster-1))
		{
			isEnd = true;
			return true;
		}
		else
			return false;
	}

	protected boolean isMonsterWin()
	{
		if(isCMAlive == true && isCHAlive == false && currentHunter == (totalHunter-1))
		{
			isEnd = true;
			return true;
		}
		else
			return false;
	}

	protected void updateHunterAliveStatus()
	{
		isCHAlive = h[currentHunter].isAlive();
	}

	protected void updateMonsterAliveStatus()
	{
		isCMAlive = m[currentMonster].isAlive();
	}

	protected void initiateMonsterAttack()
	{
		String s = m[currentHunter].hurtMH(h[currentHunter], stage);
		updateHunterAliveStatus();
		if(isCHAlive == false)
		{
			if(isMonsterWin() == true)
				s += result();
			else
			{
				currentHunter++;
				updateHunterAliveStatus();
			}
		}
		o.display(s);
	}

	protected void initiateHunterAttack()
	{
		String s = h[currentHunter].hurtMonster(m[currentMonster], stage);
		updateMonsterAliveStatus();
		if(isCMAlive == false)
		{
			if(isHunterWin() == true)
				s += result();
			else
			{
				currentMonster++;
				updateMonsterAliveStatus();
			}
		}
		o.display(s);
	}
	protected String result()
	{
		if (isHunterWin() == true)
			return ("Win");
		else
			return ("Lose");
	}
}